// classCall.js
var app = getApp();
var initdata = function (that) {
  var list = that.data.list
  for (var i = 0; i < list.length; i++) {
    list[i].txtStyle = ""
  }
  that.setData({ list: list })
}  
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: 0,
    classSonId: 0,
    teamId: 0,
    rowId: 0,
    studentId :0,
    why: "",
    list: [],
    delBtnWidth: 540,
    statusTxt: "",
    statusList: [
      {
        "imgIcon": "/img/ico/class-ico3.png",
        "statusTxt": "未到"
      },
      {
        "imgIcon": "/img/ico/class-ico2.png",
        "statusTxt": "请假"
      },
      {
        "imgIcon": "/img/ico/class-ico1.png",
        "statusTxt": "已到"
      }
    ]
  },
    // 签到
  // sign: function(e){
  //   var that = this; 
  //   var dataset = e.currentTarget.dataset;
  //   wx.request({
  //     url: app.globalData.http + "selectStudentRowClass.do?id="+that.data.optionId,
  //     header: {
  //         'content-type': 'application/json'
  //     },
  //     method: "post",
  //     data: {
  //       id: dataset.id,
  //       status: 0
  //     },
  //     success: function(res){
  //       var code = res.data.code;
  //       var txtStyle = "";
  //       var index = e.currentTarget.dataset.index;
  //       var list = that.data.list;
  //       list[index].txtStyle = txtStyle;
  //       that.data.list[dataset.index].status = 0;
  //       console.log("res---------",res);
  //       if(code == 100){
  //         that.setData({
  //           list: that.data.list
  //         })
  //         wx.showToast({
  //           title: '签到成功',
  //           icon: 'success',
  //           duration: 2000  
  //         })
  //       }else{
  //         wx.showToast({
  //           title: '签到失败',
  //           image: "/img/ico/fail.png",
  //           duration: 2000  
  //         })
  //       }  
  //       console.log(that.data.list)
  //     }
  //   })
  // },

  sign: function(e){
    var that = this; 
    var dataset = e.currentTarget.dataset;
    console.log('eeeeee------',e);
    console.log("why---",that.data.why);
    console.log("rowId---",parseInt(that.data.rowId));
    console.log("studentId---",parseInt(dataset.id));
    console.log("classSonId---",parseInt(that.data.classSonId));
    console.log("teamId---",parseInt(that.data.teamId));
    wx.request({
       url: app.globalData.http + "signIn.do",
       data: {
        why: that.data.why,
        rowId: parseInt(that.data.rowId),
        studentId: parseInt(dataset.id),
        employeesId: "",
        classSonId: parseInt(that.data.classSonId),
        teamId: parseInt(that.data.teamId),
        status: "0",
        busId: parseInt(wx.getStorageSync("busId")),
        employeesId: parseInt(wx.getStorageSync("employeesId")),
        schoolId: parseInt(wx.getStorageSync("schoolId"))
       },
       method: "GET",
       header: {
          'content-type': 'application/json'
      },
      success: function(res){
        console.log("res-------",res.data);
        var code = res.data.code;
        var txtStyle = " ";
        if(code == 100){
          that.data.list[dataset.index].status = "0";
          that.data.list[dataset.index].txtStyle = txtStyle;
          that.setData({
            list: that.data.list,
          })
          wx.showToast({
            title: '签到成功',
            icon: 'success',
            duration: 2000  
          })
        }else{
          that.data.list[dataset.index].txtStyle = txtStyle;
          wx.showToast({
            title: '签到失败',
            image: "/img/ico/fail.png",
            duration: 2000  
          })
        }  
      }
    })
  },
// 请假
  leave: function(e){
    var that = this;
    console.log("leave-----------",this.data.list[e.target.dataset.index]);
    var data = this.data.list[e.target.dataset.index];
    var txtStyle = " ";
    var studentId  = data.id;
    var studentNo = data.studentNo;
    var studentName = data.studentName;
    var classSonId = that.data.classSonId;
    var teamId = that.data.teamId;
    var rowId = that.data.rowId;
    console.log('eeeeee------',e);
    console.log("that.data.why",that.data.why);
    console.log("that.data.rowId",parseInt(that.data.rowId));
    console.log("studentId",parseInt(data.id));
    console.log("classSonId",parseInt(that.data.classSonId));
    console.log("teamId",parseInt(that.data.teamId));
    console.log("rowId",parseInt(that.data.id));
    wx.navigateTo({
      url: "/pages/classLeave/classLeave?studentId="+studentId +"&classSonId="+classSonId+"&teamId="+teamId+"&rowId="+rowId+"&studentNo="+studentNo+"&studentName="+studentName
    });
    that.data.list[e.target.dataset.index].txtStyle = txtStyle;
  },

  touchS: function(e){
    //console.log("touchS" + e);
    if (e.touches.length == 1) {
      this.setData({
        //设置触摸起始点水平方向位置  
        startX: e.touches[0].clientX
      });
    }  
  },
  touchM: function(e){
    var that = this
     //initdata(that)
    if (e.touches.length == 1) {
      //手指移动时水平方向位置  
      var moveX = e.touches[0].clientX;
      //手指起始点位置与移动期间的差值  
      var disX = this.data.startX - moveX;
      var delBtnWidth = this.data.delBtnWidth;
      var txtStyle = "";
      if (disX == 0 || disX < 0) {//如果移动距离小于等于0，文本层位置不变  
       txtStyle = "left:0px";
      } else if (disX > 0) {//移动距离大于0，文本层left值等于手指移动距离  
        txtStyle = "left:-" + disX + "rpx";
        if (disX >= delBtnWidth) {
          //控制手指移动距离最大值为删除按钮的宽度  
          txtStyle = "left:-" + delBtnWidth + "rpx";
        }
      }
      //获取手指触摸的是哪一项  
      var index = e.currentTarget.dataset.index;
      var list = this.data.list;
      //console.log(list);
      list[index].txtStyle = txtStyle ;
      //更新列表的状态  
      this.setData({
        list: list
      });
    }  
  },
  touchE: function (e) {
    if (e.changedTouches.length == 1) {
      //手指移动结束后水平位置  
      var endX = e.changedTouches[0].clientX;
      //触摸开始与结束，手指移动的距离  
      var disX = this.data.startX - endX;
      var delBtnWidth = this.data.delBtnWidth;
      //如果距离小于删除按钮的1/2，不显示删除按钮  
      var txtStyle = disX > delBtnWidth / 2 ? "left:-" + delBtnWidth + "rpx" : "left:0rpx";
      //获取手指触摸的是哪一项  
      var index = e.currentTarget.dataset.index;
      var list = this.data.list;
      list[index].txtStyle = txtStyle;
      //更新列表的状态  
      this.setData({
        list: list
      });
    }
  },
  //获取元素自适应后的实际宽度  
  getEleWidth: function (w) {
    var real = 0;
    try {
      var res = wx.getSystemInfoSync().windowWidth;
      var scale = (750 / 2) / (w / 2);//以宽度750px设计稿做宽度的自适应  
      // console.log(scale);  
      real = Math.floor(res / scale);
      return real;
    } catch (e) {
      return false;
      // Do something when catch error  
    }
  },
  initEleWidth: function () {
    var delBtnWidth = this.getEleWidth(this.data.delBtnWidth);
    this.setData({
      delBtnWidth: delBtnWidth
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.initEleWidth();  
    var that = this;
    console.log("班级学员页面加载options--",options);
    wx.request({
      url: app.globalData.http + "selectStudentRowClass.do",
      data:{
        id: parseInt(options.rowId),
        busId: parseInt(wx.getStorageSync("busId")),
        employeesId: parseInt(wx.getStorageSync("employeesId")),
        schoolId: parseInt(wx.getStorageSync("schoolId"))
      },
      header: {
          'content-type': 'application/json'
      },
      method: "GET",
      success: function(res){
        console.log("班级学员请求成功---",res.data);
        var code = res.data.code;
        if(code == 100){
            that.setData({
              list:res.data.data,
              classSonId:options.classSonId,
              teamId:options.teamId,
              rowId:options.rowId
            });
        }
        if(code == 200){
          that.setData({
            codeTxt: "暂时没有数据"
          })
        }
        if(code == 300){
          that.setData({
            codeTxt: "哇哦~接口异常了"
          })
        }
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})